﻿module LabyrinthGameState

open SI
open Tile
open Chessboard
open Player
open Microsoft.Xna.Framework.Graphics
open Math
open System
open PathFinding

type State = 
    {
        mTileList               : Tile array
        mChessboard             : Chessboard
        mPlayers                : Player list
        mutable mSelectedPlayer : Player
        mutable mWinner         : bool
        mutable mNextTurn       : bool
        mutable mFirstPart      : bool
        mutable mRemainedTile   : Tile
        mutable mSelectedTile   : Tile 
    }

    static member RemainingTile (tileList : Tile array) = 
        let mutable res = tileList.[0]
        for item in tileList do
            if item.mPosition.X = -1.f<tile> && item.mPosition.Y = -1.f<tile> then
                res <- item
        res

    static member RandomOrientation(rand : System.Random) = 
        let value = rand.Next() % 4;
        if value = 0 then
            ORIENTATION.N
        else if value = 1 then
            ORIENTATION.S
        else if value = 2 then
            ORIENTATION.W
        else if value = 3 then
            ORIENTATION.E
        else
            ORIENTATION.N

    static member TileListInit(dimension) = 
        let tileList = Array.zeroCreate 50
        let rand = new Random()
        for i in 0..49 do
            let mutable pos = Vector2<tile>(0.f<tile>,0.f<tile>)
            if i = 0 then
                pos.X <- 0.f<tile>
                pos.Y <- 0.f<tile>
                tileList.[0] <- Tile.Init(i, pos, TILE_TYPE.Corner, ORIENTATION.E, dimension)
            else if i = 1 then
                pos.X <- 0.f<tile>
                pos.Y <- 6.f<tile>
                tileList.[1] <- Tile.Init(i, pos, TILE_TYPE.Corner, ORIENTATION.S, dimension)
            else if i = 2 then
                pos.X <- 6.f<tile>
                pos.Y <- 0.f<tile>
                tileList.[2] <- Tile.Init(i, pos, TILE_TYPE.Corner, ORIENTATION.N, dimension)
            else if i = 3 then
                pos.X <- 6.f<tile>
                pos.Y <- 6.f<tile>
                tileList.[3] <- Tile.Init(i, pos, TILE_TYPE.Corner, ORIENTATION.W, dimension)
            else
                pos.X <- -1.f<tile>
                pos.Y <- -1.f<tile>
                if (i > 3 && i < 19) then
                    tileList.[i] <- Tile.Init(i, pos, TILE_TYPE.Curved, State.RandomOrientation(rand), dimension)
                if (i > 18 && i < 34) then
                    tileList.[i] <- Tile.Init(i, pos, TILE_TYPE.Streight, State.RandomOrientation(rand), dimension)
                if (i > 33 && i < 50) then
                    tileList.[i] <- Tile.Init(i, pos, TILE_TYPE.Tie, State.RandomOrientation(rand), dimension)
        tileList

    static member PlayersInit(numPlayers, position : Tile list, goals , firstGoal) = 
        let mutable players = []
        let mutable counter = numPlayers
        if (counter > 0) then
            players <- Player.InitPlayer(Microsoft.Xna.Framework.Color.Red, position.[0], PathFinder.CreatePathFinder(), goals, firstGoal) :: players
        counter <- counter - 1
        if (counter > 0) then
            players <- Player.InitPlayer(Microsoft.Xna.Framework.Color.Blue, position.[1], PathFinder.CreatePathFinder(), goals, firstGoal) :: players
        counter <- counter - 1
        if (counter > 0) then
            players <- Player.InitPlayer(Microsoft.Xna.Framework.Color.Green, position.[2], PathFinder.CreatePathFinder(), goals, firstGoal) :: players
        players

    static member Init(numPlayers, goals, tileDimension) =      
        let tileList = State.TileListInit(tileDimension)
        let chessboard = Chessboard.Init(tileList)
        let corners = 
            [
                chessboard.board.[0]
                chessboard.board.[6]
                chessboard.board.[42]
                chessboard.board.[48]
            ]
        let players = State.PlayersInit(numPlayers, corners, goals, chessboard.board.[0])
        let res = 
            {
                mTileList       = tileList
                mChessboard     = chessboard
                mPlayers        = players
                mSelectedPlayer = players.[0]
                mFirstPart      = true
                mWinner         = false
                mNextTurn       = true
                mRemainedTile   = State.RemainingTile(tileList)
                mSelectedTile   = tileList.[0]
            }
        res


let mutable gameState = State.Init(1, 3, 100.f<pixel>)